<?php
namespace app\api\controller;

use app\admin\controller\Goods;
use app\BaseController;
use app\admin\model\Artist;
use app\admin\model\ArtistFollow as ArtistFollowModel;
use app\admin\model\Goods as ModelGoods;

/**
 * @Apidoc\Group("艺术家")
 */
class ArtistFollow extends ApiBase
{
    /**
     * @Apidoc\Title("艺术家关注/取消")
     * @Apidoc\Url("/api/artist_follow")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("artist_id", type="int", require=true, desc="艺术家ID")
     * @Apidoc\Param("type", type="int", desc="1关注 2取消")
     */
    public function follow()
    {
        $artist_id = param_check('artist_id');
        $type = request()->param('type/d', 1);
        $uid = $this->get_user_id();

        if (!Artist::where('id', $artist_id)->value('id')) {
            return json_response(400, '艺术家不存在');
        }

        $record = ArtistFollowModel::where(['uid' => $uid, 'artist_id' => $artist_id])->find();

        if ($type == 1) {
            if ($record) {
                return json_response(400, '已关注');
            }
            try {
                ArtistFollowModel::create(['uid' => $uid, 'artist_id' => $artist_id]);
            } catch (\Exception $e) {
                return json_response(400, '关注失败：' . $e->getMessage());
            }
        } else {
            if (!$record) {
                return json_response(400, '未关注，无需取消');
            }
            $record->delete();
        }

        return json_response(200, '操作成功');
    }

    /**
     * @Apidoc\Title("我的艺术家关注列表")
     * @Apidoc\Url("/api/my_artist_follow")
     * @Apidoc\Method("GET")
     * @Apidoc\Param("page", type="int", desc="页码")
     * @Apidoc\Param("limit", type="int", desc="每页数量")
     * @Apidoc\Param("name", type="string", desc="艺术家姓名筛选")
     * @Apidoc\Returned("[]", type="object", desc="艺术家列表",
     *     @Apidoc\Returned("artist_id", type="int", desc="艺术家ID"),
     *     @Apidoc\Returned("name", type="string", desc="姓名"),
     *     @Apidoc\Returned("avatar", type="string", desc="头像"),
     *     @Apidoc\Returned("title", type="string", desc="头衔")
     * )
     */
    public function myFollow()
    {
        $uid   = $this->get_user_id();
        $page  = request()->get('page/d', 1);
        $limit = request()->get('limit/d', 10);
        $name  = request()->get('name', '');

        $where = [['uid', '=', $uid]];

        if ($name) {
            $aids = Artist::where('name', 'like', "%$name%")->column('id');
            $where[] = ['artist_id', 'in', $aids ?: [0]];
        }

        $list = ArtistFollowModel::with('artist')
            ->where($where)
            ->field('id, artist_id')
            ->page($page, $limit)
            ->order('id', 'desc')
            ->select();

        return json_response(200, 'success', $list);
    }

    //艺术家详情
    public function artist_detail(){
        $artist_id = param_check('artist_id');
        $artist = Artist::where('id', $artist_id)->find();
        $artist->is_follow = ArtistFollowModel::where(['uid' => $this->get_user_id(), 'artist_id' => $artist_id])->count() > 0;
        if (!$artist) {
            return json_response(400, '艺术家不存在');
        }

        return json_response(200, 'success', $artist);
    }


    //艺术家作品
    // public function artist_works(){
    //     $artist_id = param_check('artist_id');
    //     $page = request()->get('page/d', 1);
    //     $limit = request()->get('limit/d', 10);
    //     $type = request()->get('type/d', 1); //1 实物 2数字珍品
    //     $works = Artist::where('id', $artist_id)
    //         ->with(['works' => function($query) use ($page, $limit,$type) {
    //             $where = [];
    //             $where[] = ['is_del', '=', 0];
    //             if($type > 0){
    //                 $where[] = ['goods_type', '=', $type];
    //             }
    //             $query->field('id, store_name, image, price,cate_id,artist_id')
    //                   ->where($where)
    //                   ->page($page, $limit)
    //                   ->with(['cate'])
    //                   ->order('id', 'desc');
    //         }])
    //         ->find();

    //     if (!$works) {
    //         return json_response(400, '艺术家或作品不存在');
    //     }

    //     return json_response(200, 'success', $works);
    // }


    public function artist_works(){
        $artist_id = param_check('artist_id');
        $page = request()->get('page/d', 1);
        $limit = request()->get('limit/d', 10);
        $type = request()->get('type/d', 1); //1 实物 2数字珍品
        $works = ModelGoods::where('artist_id', $artist_id)
            ->where('is_del', 0)
            ->where('goods_type', $type)
            ->field('id, store_name, image, price, cate_id, artist_id,goods_type')
            ->page($page, $limit)
            ->with(['cate','artist'])
            ->order('id', 'desc')
            ->select();
        return json_response(200, 'success', $works);
    }
}
